﻿using Excel2DbTool.Common;
using MvvmFoundation.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Excel2DbTool.Helpers;
using System.Data;
using System.Windows;

namespace Excel2DbTool.ViewModels
{
    public class FilePathViewModel : ObservableObject
    {
        BackgroundWorker backgroundWorker;

        public FilePathViewModel(String filePath)
        {
            Val = filePath;
            backgroundWorker = new BackgroundWorker();
        }

        public String Val { get; private set; }

        private Boolean isProccessing = false;
        public Boolean IsProccessing
        {
            get
            {
                return isProccessing;
            }
            set
            {
                if (isProccessing == value)
                {
                    return;
                }

                isProccessing = value;

                RaisePropertyChanged("IsProccessing");
            }
        }


        private Boolean isFail = false;
        public Boolean IsFail
        {
            get
            {
                return isFail;
            }
            set
            {
                if (isFail == value)
                {
                    return;
                }

                isFail = value;

                RaisePropertyChanged("IsFail");
            }
        }


        public void Reset() {
            this.IsProccessing = false;
            this.IsFail = false;
        }

        public void Execute()
        {
            inilitiseBackgroundWorker();

            backgroundWorker.RunWorkerAsync();
        }

        void inilitiseBackgroundWorker()
        {
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;

        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            IsProccessing = true;
            
            Excel2DbTempTable();
            DataMigrater();
        }

        void Excel2DbTempTable()
        {
            var helper = new ExcelHelper(this.Val);
            var dt = helper.ExcelToDataTable("", true);
            var recordGroup = dt.ToAGroupOfExcelRowRecord();

            SqlHelper.GetInstance().BatchInsertRecord(recordGroup);

        }

        void DataMigrater()
        {
            Excel2DbTool.Migrater_New.XMTableMigrater d = new Excel2DbTool.Migrater_New.XMTableMigrater();
            d.Execute();
        }

        void TestTempTable2FiveTable()
        {
            Excel2DbTool.Migrater_New.XMTableMigrater d = new Excel2DbTool.Migrater_New.XMTableMigrater();
            d.Execute();
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.IsFail = true;
                IsProccessing = false;


                App.Messenger.NotifyColleagues("Fail");
                throw new Excel2DbException(e.Error.Message, e.Error);
            }

            App.Messenger.NotifyColleagues("Done");
        }
    }
}
